package cn.zhouchao.org.aop;

import cn.zhouchao.org.domain.DepartmentLog;
import cn.zhouchao.org.mapper.DepartmentLogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Date;

@Aspect
@Component
public class DpartmentManager {
    @Autowired
    private DepartmentLogMapper departmentLogMapper;

    @Pointcut("@annotation(cn.zhouchao.org.aop.DepartmentLogAop)")
    public void pointCut(){}
    @Around("pointCut()")
    public Object DepartmentLog(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        DepartmentLogAop departmentLog = method.getAnnotation(DepartmentLogAop.class);
        String methodName = departmentLog.methodName();
        String methodType = departmentLog.methodType();
        //方法名
        String name = method.getName();
        InetAddress localHost = Inet4Address.getLocalHost();
        String hostAddress = localHost.getHostAddress();
        Object proceed = pjp.proceed();

        departmentLogMapper.add(new DepartmentLog(null, name, methodName, methodType, hostAddress, new Date()));

        return proceed;

    }
}
